/**
 * Created with JetBrains WebStorm.
 * User: scb
 * Date: 25/4/13
 * Time: 12:20 PM
 * To change this template use File | Settings | File Templates.
 */

describe("MailboxListController",function(){
    var mailboxListController = null;
    var t_alertCtrl = null;
    var response = {
        "mailListResponse":{
            "mailList":[
                {"selected":false,"sender":"","id":782084,"senderAddressType":"STANDARD_CHARTERED_BANK","category":"FAILED","receiverId":"8521224863","subject":"Transaction Failure","status":"N","statusDate":"2012-01-30T21:35:01.882+0800","dateCreated":"2012-01-30T21:35:01.882+0800"},
                {"selected":false,"sender":"SCHEDULER","id":779892,"senderAddressType":"STANDARD_CHARTERED_BANK","category":"FAILED","receiverId":"8521224863","subject":"Transaction Failure","status":"N","statusDate":"2009-12-03T15:09:55.203+0800","dateCreated":"2009-12-03T15:09:55.203+0800"},
                {"selected":false,"sender":"SCHEDULER","id":779771,"senderAddressType":"STANDARD_CHARTERED_BANK","category":"FAILED","receiverId":"8521224863","subject":"Transaction Failure","status":"R","statusDate":"2008-11-27T10:08:13.217+0800","dateCreated":"2012-01-30T18:36:14.287+0800"},
                {"selected":false,"sender":"SCHEDULER","id":779681,"senderAddressType":"STANDARD_CHARTERED_BANK","category":"FAILED","receiverId":"8521224863","subject":"Transaction Failure","status":"R","statusDate":"2009-11-26T14:08:37.362+0800","dateCreated":"2009-11-24T14:28:28.514+0800"},
                {"selected":false,"sender":"","id":776725,"senderAddressType":"STANDARD_CHARTERED_BANK","category":"FAILED","receiverId":"8521224863","subject":"Transaction Failure","status":"R","statusDate":"2008-10-30T18:08:10.852+0800","dateCreated":"2008-10-30T18:08:10.852+0800"}],
            "sendMailList":[
                {"selected":false,"sender":"8521224863","id":782911,"senderAddressType":"","category":"2","receiverId":"","subject":"ssasas","status":"N","statusDate":"2013-04-13T17:57:11.746+0800","dateCreated":"2013-04-13T17:57:11.746+0800"},
                {"selected":false,"sender":"8521224863","id":782910,"senderAddressType":"","category":"3","receiverId":"","subject":"hi","status":"N","statusDate":"2013-04-13T15:23:44.875+0800","dateCreated":"2013-04-13T15:23:44.875+0800"}
            ],"responseHeader":{"status":1,"currentTimestamp":"2013-04-13T18:04:11.724+0800","nonce":"a276d184df5c11eb486ef49bfea5edf7"}
        }
    };
    
    beforeEach(function(){
        //need feature
        t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();
        App.MailboxListController.reopen({
            init:function(){
                this.set('controllers', Ember.Object.create({
                    alert: t_alertCtrl,
                    loading: t_loadingCtrl
                }));
            }
        });
        mailboxListController = App.MailboxListController.create();
    });
    
    it("prepareController if ",function() {
        App.RouteHistory.lastRoute = "mailbox.compose";
        App.Router.setParameter({});
        spyOn(mailboxListController,"setMailType");
        mailboxListController.prepareController();
        expect(mailboxListController.setMailType).toHaveBeenCalledWith(false);
    });

    it("prepareController else ",function() {
        App.RouteHistory.lastRoute = "mailbox.list";
        App.Router.setParameter(null);
        spyOn(mailboxListController,"setMailType");
        mailboxListController.prepareController();
        expect(mailboxListController.setMailType).toHaveBeenCalledWith(true);
    });

    it("setMailType return",function() {
    	mailboxListController.set('currentSelected', 'inbox');
        spyOn(mailboxListController,"fetchInboxList");
        mailboxListController.setMailType(true);
        expect(mailboxListController.fetchInboxList).not.toHaveBeenCalled();
    });
    
    it("setMailType inbox = true",function() {
        spyOn(mailboxListController,"fetchInboxList");
        mailboxListController.setMailType(true);
        expect(mailboxListController.fetchInboxList).toHaveBeenCalled();
    });

    it("setMailType inbox = false",function() {
        spyOn(mailboxListController,"fetchSentList");
        mailboxListController.setMailType(false);
        expect(mailboxListController.fetchSentList).toHaveBeenCalled();
    });

    it("fetchInboxList",function() {
        spyOn(App.apiMailBox,"getInbox").andCallFake(function(callback) {
            callback({});
        });
        spyOn(mailboxListController,"listCallback");
        mailboxListController.fetchInboxList();
        expect(App.apiMailBox.getInbox).toHaveBeenCalled();
        expect(mailboxListController.listCallback).toHaveBeenCalledWith({}, true);
    });
    
    it("fetchSentList",function() {
        spyOn(App.apiMailBox,"getSent").andCallFake(function(callback) {
            callback({});
        });
        spyOn(mailboxListController,"listCallback");
        mailboxListController.fetchSentList();
        expect(App.apiMailBox.getSent).toHaveBeenCalled();
        expect(mailboxListController.listCallback).toHaveBeenCalledWith({}, false);
    });

    it("listCallback",function() {
    	var list = [
            {'2013-07-01': [App.InboxMessage.create({"status": "N"})]},
    	    {'2013-07-02': [App.InboxMessage.create({"status": "N"})]}
        ];
    	list['2013-07-01'] = [];
    	list['2013-07-01'].push(App.InboxMessage.create({"status": "N"}));
    	list['2013-07-02'] = [];
    	list['2013-07-02'].push(App.InboxMessage.create({"status": "N"}));
    	mailboxListController.set('listData', []);
        mailboxListController.listCallback(list, true);
        expect(mailboxListController.get('noOfUnReadEmails')).toEqual(2);
    });
    
    it("selectItem",function() {
        spyOn(App.apiMailBox,"mailDetail").andCallFake(function(obj,callback) {
            callback(obj,{});
        });
        spyOn(mailboxListController,"callbackMailDetail");
        mailboxListController.selectItem({});
        expect(App.apiMailBox.mailDetail).toHaveBeenCalled();
        expect(mailboxListController.callbackMailDetail).toHaveBeenCalled();
    });

    it("callbackMailDetail Inbox",function() {
    	var message = App.InboxMessage.create();
    	message.set("isRead", false);
    	mailboxListController.set("noOfUnReadEmails", 2);
    	spyOn(mailboxListController, "transitionTo");
    	spyOn(mailboxListController, "hideLoading");
    	
        mailboxListController.callbackMailDetail(message, {}, null);
        
        expect(mailboxListController.get('noOfUnReadEmails')).toEqual(1);
        expect(mailboxListController.transitionTo).toHaveBeenCalled();
        expect(mailboxListController.hideLoading).toHaveBeenCalled();
    });
    
    it("callbackMailDetail Sent",function() {
    	var message = App.SentMessage.create();
    	message.set("isRead", false);
    	mailboxListController.set("noOfUnReadEmails", 0);
    	spyOn(mailboxListController, "transitionTo");
    	spyOn(mailboxListController, "hideLoading");
    	
        mailboxListController.callbackMailDetail(message, {}, null);
        
        expect(mailboxListController.get('noOfUnReadEmails')).toEqual(0);
        expect(mailboxListController.transitionTo).toHaveBeenCalled();
        expect(mailboxListController.hideLoading).toHaveBeenCalled();
    });

    it("showCompose",function() {
        spyOn(mailboxListController,"transitionTo");
        mailboxListController.showCompose(mailboxListController);
        expect(mailboxListController.transitionTo).toHaveBeenCalledWith('mailbox.compose');
    });
    
});